package org.emavaj.sanguosha.controller.client;

import org.emavaj.common.JSONUtil;
import org.emavaj.sanguosha.model.constants.Opercation;
import org.emavaj.sanguosha.model.vo.SGSPacket;

/**
 * User: dizem
 * Time: 11-4-28 下午3:40
 * 
 * eMavaj updated 20120914
 */
public class ClientDispatcher {

	private GameClient client;

	public ClientDispatcher(GameClient client) {
		this.client = client;
	}

	public void dispatch(SGSPacket packet) {

		if (packet.is(Opercation.OP_INIT_CLIENT)) {//客户端接入
			this.client.showGameFrame(packet);
			
		} else if (packet.is(Opercation.OP_UPDATE_PLAYERS)) {//添加玩家
			this.client.updatePlayers(packet);

		} else if (packet.is(Opercation.OP_SEND_MESSAGE)) {//系统消息
			this.client.showMessage(packet.getMessage());

		} else if (packet.is(Opercation.OP_SEND_CHAT_MESSAGE)) {//聊天消息
			this.client.showChatMessage(packet.getMessage());

		} else if (packet.is(Opercation.OP_DISTRIBUTE_ROLE)) {//分配角色
			this.client.setRole(packet.getRole(), packet.getLordId());

		} else if (packet.is(Opercation.OP_DISTRIBUTE_LORD_CHARACTER)) {//分配主公武将
			this.client.distributeLordCharacter(packet);

		} else if (packet.is(Opercation.OP_DISTRIBUTE_CHARACTER)) {//分配其他玩家武将
			this.client.distributeCharacter(packet);

		} else if (packet.is(Opercation.OP_DISTRIBUTE_CARD)) {//发牌
			this.client.distributeCards(packet);

		} else if (packet.is(Opercation.OP_FINISH_CHOOSING_CHARACTER)) {//角色选择完毕
			this.client.setCharacter(packet);

		} else if (packet.is(Opercation.OP_UPDATE_PLAYERS_INFO)) {//更新玩家信息
			this.client.updatePlayersInfo(packet);

		} else if (packet.is(Opercation.OP_PHASE_START)) {//开始阶段
			client.startPhase(packet.getPlayerId());

		} else if (packet.is(Opercation.OP_PHASE_JUDGE_BEGIN)) {//判定阶段
			client.judgePhase(packet.getPlayerId());

		} else if (packet.is(Opercation.OP_PHASE_DRAW_BEGIN)) {//摸牌阶段
			client.drawPhase(packet);

		} else if (packet.is(Opercation.OP_PHASE_PLAY_BEGIN)) {//出牌阶段
			client.playPhase(packet.getPlayerId());

		} else if (packet.is(Opercation.OP_OFFER_CARD_TO)) {//向某人出牌
			client.beOfferredCardTo(packet);

		} else if (packet.is(Opercation.OP_FEEDBACK)) {
			client.feedback(packet);

		} else if (packet.is(Opercation.OP_DECREASE_LIFE)) {//减血
			client.decreaseLife(packet);

		} else if (packet.is(Opercation.OP_PHASE_DISCARD_BEGIN)) {//弃牌阶段开始
			client.discard(packet);

		} else if (packet.is(Opercation.OP_DISCARD)) {//弃牌
			client.discardEnd(packet);

		} else if (packet.is(Opercation.OP_EAT_PEACH)) {
			client.eatPeach(packet);

		} else if (packet.is(Opercation.OP_ADD_EQUIPMENT)) {
			client.addEquipmentCard(packet);
		}

	}

	public void dispatch(String jsonString) {
		SGSPacket packet = (SGSPacket) JSONUtil.convertToObject(jsonString, SGSPacket.class);
		dispatch(packet);
	}
}
